<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <!-- <el-form-item label="工具名称">
          <el-input
            v-model="formInline.name"
            placeholder="请输入项目名称"
          ></el-input>
        </el-form-item> -->
        <!-- <el-form-item label="是否显示" class="movement">
          <el-select v-model="formInline.status" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="时间">
          <el-date-picker v-model="formInline.value" type="daterange" range-separator="至" start-placeholder="开始日期"
            end-placeholder="结束日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item> -->
        <!-- <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增分类</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="text item">
      <el-table
        v-loading="loading"
        :data="tableData"
        max-height="600"
        border
        style="width: 100%"
      >
        <el-table-column
          prop="index"
          align="center"
          :index="indexMethod"
          type="index"
          label="序号"
          width="90"
        >
        </el-table-column>
        <el-table-column
          prop="name"
          align="center"
          label="分类名称"
          width="180"
        >
        </el-table-column>
        <!-- <el-table-column prop="img" align="center" label="项目图片">
          <template slot-scope="scope">
            <el-image
              style="width: 50px; height: 50px"
              :src="scope.row.img"
              fit="cover"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="common" align="center" label="排序">
        </el-table-column> -->
        <el-table-column prop="ctime" align="center" label="创建时间">
        </el-table-column>
        <el-table-column prop="status" align="center" label="是否显示">
          <template slot-scope="scope">
            <span>{{ scope.row.status == "1" ? "是" : "否" }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.status == 1"
              type="danger"
              size="mini"
              @click="handleQiYong(scope.row)"
              >禁用</el-button
            >
            <el-button
              v-if="scope.row.status != 1"
              type="primary"
              size="mini"
              @click="handleQiYong(scope.row)"
              >启用</el-button
            >
            <el-button
              v-if="scope.row.status == 2"
              type="danger"
              size="mini"
              @click="handleDel(scope.row)"
              >删除</el-button
            >
            <el-button
              v-if="scope.row.status == 2"
              type="warning"
              size="mini"
              @click="handleEdit(scope.row)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[50, 100, 200, 300]"
        :page-size="50"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog
      :visible.sync="dialogVisible"
      width="40%"
      :before-close="handleClose"
      :title="titleText"
    >
      <el-form
        :model="formLabelAlign"
        label-width="85px"
        :rules="rules"
        ref="form"
      >
        <div class="oneLine">
          <el-form-item label="分类名称" prop="name">
            <el-input
              v-model="formLabelAlign.name"
              placeholder="请输入项目名称"
            ></el-input>
          </el-form-item>
        </div>
        <!-- <div class="oneLine">
          <el-form-item label="排序" prop="type">
            <el-select v-model="value" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </div> -->
        <!-- <div class="oneLine">
          <el-form-item label="头像" prop="imageUrl">
            <div class="unlodBox">
              <el-upload
                class="upload-demo"
                action
                :http-request="uploadFiles"
                ref="upload"
                :limit="fileLimit"
                :on-remove="handleRemove"
                :file-list="fileList"
                :on-exceed="handleExceed"
                :before-upload="beforeUpload"
                :show-file-list="false"
                :headers="headers"
              >
                <el-image
                  v-if="imageUrl"
                  class="imageGood"
                  :src="imageUrl"
                  style="width: 360px; height: 180px"
                ></el-image>
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                <div slot="tip" class="el-upload__tip">
                  只能上传jpg/png文件，且不超过5M
                </div>
              </el-upload>
            </div>
          </el-form-item>
        </div> -->
      </el-form>
      <div class="btnBox">
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="save">确 认</el-button>
          <el-button @click="handleClose">返 回</el-button>
        </span>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import { opinions, uploadFiles } from "../../../../utils/api";
import {
  getGongJufL,
  addGongJufL,
  editGongJufL,
  delGongJufL,
  QiYong,
} from "../../../../utils/main";
export default {
  data() {
    return {
      options: [
        {
          value: "1",
          label: "1",
        },
        {
          value: "2",
          label: "2",
        },
      ],
      formLabelAlign: {
        name: "",
        tel: "",
        avatar: "",
      },
      fileLimit: 1,
      //   上传图片
      fileList: [],
      headers: { "Content-Type": "multipart/form-data" },
      loading: false,
      token: sessionStorage.getItem("token"),
      dialogVisible: false,
      formInline: {
        mobile: "",
        name: "",
      },
      tableData: [],
      parameter: {
        common: "",
      },
      total: 0,
      limit: 50,
      page: 1,
      name: "",
      type: "2",
      status: "1",
      titleText: "",
      currentPage: 1,
      value: "",
      rules: {
        name: [
          { required: true, message: "请输入工具分类名称", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    // 启用禁用接口
    conditionUser(id, status) {
      QiYong(this.token, id, status)
        .then((res) => {
          if (res.data.status) {
            this.getList();
          } else {
            if (res.data.msg == "token失效,请重新进行登录") {
              this.$router.push("/login");
            }
            if (res.data.msg == "此账号没有权限") {
              this.$message.error(res.data.msg);
              this.loading = false;
            }
            let timer = setTimeout(() => {
              this.$message.error(res.data.msg);
            }, 1000);
          }
        })
        .catch((err) => {
          console.log(err.message);
        });
    },
    //启用禁用
    handleQiYong(row) {
      if (row.status == 0) {
        let status = 2;
        this.conditionUser(row.id, status);
      }
      if (row.status == 1) {
        let status = 2;
        this.conditionUser(row.id, status);
      }
      if (row.status == 2) {
        let status = 1;
        this.conditionUser(row.id, status);
      }
      // this.getList();
    },
    //查询
    onSubmit() {
      //   this.opinionsS();
      this.getList();
    },
    //详情
    // handleEdit(row) {
    //   this.parameter.common = row.common;
    //   this.dialogVisible = true;
    // },
    // 增加
    handleAdd() {
      this.formLabelAlign = {};
      this.dialogVisible = true;
      this.titleText = "新增工具分类";
    },
    // 编辑
    handleEdit(row) {
      let obj = JSON.parse(JSON.stringify(row));
      this.formLabelAlign = obj;
      this.dialogVisible = true;
      this.titleText = "编辑工具分类";
    },
    save() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          // console.log(this.formLabelAlign, "8888880");
          if (this.formLabelAlign.id != null) {
            // console.log(this.formLabelAlign, "000");
            // 编辑
            editGongJufL(
              this.token,
              this.formLabelAlign.name,
              this.formLabelAlign.status,
              this.type,
              this.formLabelAlign.id
            ).then((res) => {
              if (res.data.status) {
                this.$message({
                  message: "修改成功",
                });
                this.dialogVisible = false;
                this.getList();
              } else {
                this.$message.error(res.data.msg);
              }
            });
          } else {
            // console.log(this.formLabelAlign, "111");
            // 新增
            addGongJufL(
              this.token,
              this.formLabelAlign.name,
              this.status,
              this.type
            ).then((res) => {
              if (res.data.status) {
                this.$message({
                  message: "新增成功",
                });
                this.total = res.data.data.count;
                this.formLabelAlign = {};
                this.dialogVisible = false;
                this.getList();
              } else {
                this.$message.error(res.data.msg);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    // 删除
    handleDel(row) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // console.log(row.id);
          delGongJufL(this.token, row.id).then((res) => {
            if (res.data.status) {
              this.$message({
                type: "success",
                message: "删除成功",
              });
              this.getList();
            } else {
              this.$message.error(res.data.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //序号
    indexMethod(index) {
      if (this.total < this.limit) {
        return this.total - index;
      } else {
        return this.total - (this.page - 1) * this.limit - index;
      }
    },
    //关闭
    handleClose() {
      this.$refs.form.resetFields();
      this.dialogVisible = false;
    },
    //条数
    handleSizeChange(val) {
      this.limit = val;
      //   this.opinionsS();
      this.getList();
    },
    //页数
    handleCurrentChange(val) {
      this.page = val;
      //   this.opinionsS();
      this.getList();
    },
    // 新增
    handleRemove() {},
    //超出文件个数的回调
    // handleExceed() {
    //   this.$message({
    //     type: "warning",
    //     message: "超出最大上传文件数量的限制！",
    //   });
    //   return;
    // },
    //上传文件的格式
    // beforeUpload(file) {
    //   if (file.type != "" || file.type != null || file.type != undefined) {
    //     //截取文件的后缀，判断文件类型
    //     const FileExt = file.name.replace(/.+\./, "").toLowerCase();
    //     //计算文件的大小
    //     const isLt5M = file.size / 1024 / 1024 < 1; //这里做文件大小限制
    //     //如果大于50M
    //     if (!isLt5M) {
    //       this.$message.error("上传文件大小不能超过 1MB!");
    //       return false;
    //     }
    //     //如果文件类型不在允许上传的范围内
    //     if (this.fileType.includes(FileExt)) {
    //       return true;
    //     } else {
    //       this.$message.error("上传文件格式不正确!");
    //       return false;
    //     }
    //   }
    // },
    //上传文件的事件
    // uploadFiles(item) {
    //   this.$message("文件上传中........");
    //   //上传文件的需要formdata类型;所以要转
    //   uploadFiles(item.file).then((res) => {
    //     if (res.data.status != false || res.data.id != null) {
    //       console.log(res);
    //       res.data.data.saveurl; //半截
    //       res.data.data.weburl; //全拼
    //       this.imageUrl1 = res.data.data.saveurl;
    //       this.imageUrl = res.data.data.weburl;
    //       this.fileList.push(item.file); //成功过后手动将文件添加到展示列表里
    //       let i = this.fileList.indexOf(item.file);
    //       this.handleSuccess();
    //     }
    //   });
    // },
    //列表数据
    // opinionsS() {
    //   this.loading = true;
    //   opinions(
    //     this.token,
    //     this.page,
    //     this.limit,
    //     this.value,
    //     this.formInline.mobile,
    //     this.formInline.name
    //   )
    //     .then((res) => {
    //       if (res.data.status) {
    //         this.tableData = res.data.data.data;
    //         this.total = res.data.data.count;
    //         this.tableData.forEach((item) => {
    //           if (item.avatar) {
    //             item.image = item.avatar;
    //             item.img = "https://bjsf.bjsky.top" + item.avatar;
    //           }
    //           if (item.ctime) {
    //             item.ctime_text = this.getNowTime(item.ctime * 1000);
    //           }
    //         });
    //         let timer = setTimeout(() => {
    //           this.loading = false;
    //           clearTimeout(timer);
    //         }, 400);
    //       } else {
    //         if (res.data.msg == "token失效,请重新进行登录") {
    //           this.$router.push("/login");
    //         }
    //         if (res.data.msg == "此账号没有权限") {
    //           this.$message.error(res.data.msg);
    //           this.loading = false;
    //         }
    //         let timer = setTimeout(() => {
    //           this.$message.error(res.data.msg);
    //         }, 5000);
    //       }
    //     })
    //     .catch((err) => {
    //       console.log(err.message);
    //     });
    // },
    async getList() {
      this.loading = true;
      let res = await getGongJufL(
        this.token,
        this.page,
        this.limit,
        this.name,
        this.type
      );
      if (res.data.status) {
        this.tableData = res.data.data.data || [];
        this.total = res.data.data.count;
        // console.log(this.total);
        // console.log(this.tableData, "55555");
        let timer = setTimeout(() => {
          this.loading = false;
          clearTimeout(timer);
        }, 400);
      } else {
        if (res.data.msg == "token失效,请重新进行登录") {
          this.$router.push("/login");
        }
        if (res.data.msg == "此账号没有权限") {
          this.$message.error(res.data.msg);
          this.loading = false;
        }
        let timer = setTimeout(() => {
          this.$message.error(res.data.msg);
        }, 5000);
      }
    },
    //     getNowTime(e) {
    //       let date = new Date(e);
    //       let year = date.getFullYear();
    //       let month = date.getMonth() + 1;
    //       let day = date.getDate();
    //       let hour = date.getHours();
    //       let minute = date.getMinutes();
    //       month = month < 10 ? "0" + month : month;
    //       day = day < 10 ? "0" + day : day;
    //       hour = hour < 10 ? "0" + hour : hour;
    //       minute = minute < 10 ? "0" + minute : minute;
    //       return year + "-" + month + "-" + day + " " + hour + ":" + minute;
    //     },
  },
  mounted() {
    // this.opinionsS();
    this.getList();
  },
};
</script>

<style scoped>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}

.box-card {
  width: 100%;
}

.divFlex {
  display: flex;
  align-items: center;
}
.oneLine {
  display: flex;
  justify-content: space-between;
  margin-bottom: 14px;
}
.addP {
  font-size: 22px;
  text-align: center;
  margin-bottom: 10px;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.btnBox {
  display: flex;
  justify-content: center;
}
</style>